package cn.com.gridinfo.sooc.core.expand.callname.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jeremy.arad.utils.AnimUtil;
import com.jeremy.arad.utils.Log;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.base.BaseSoocFragment;
import cn.com.gridinfo.sooc.core.expand.callname.activity.SigninStateActivity;
import cn.com.gridinfo.sooc.core.expand.callname.adapter.SignPhotoAdapter;
import cn.com.gridinfo.sooc.core.expand.callname.bean.Student;
import cn.com.gridinfo.sooc.core.expand.callname.common.CodeConst;
import cn.com.gridinfo.sooc.core.expand.callname.common.PageConst;
import cn.com.gridinfo.sooc.core.expand.callname.dao.CallNameDao;
import cn.com.gridinfo.sooc.utils.MessageUtils;

/**
 * 实到 签到图片
 * Created by yingchunli on 2015/12/23.
 */
public class ShiDaoSignPhotoFragment extends BaseSoocFragment {

    View view;//当前界面
    @InjectView(R.id.signphoto_shidao)
    GridView photoGridView;//签到照片
    SignPhotoAdapter adapter;//适配器
    CallNameDao dao;
    String ktid;//课堂id
    String kcid;//课程id
    List<Student> list = new ArrayList<>();//列表数据
    private static int INTENT_SIGNINSTATE = 90;//跳转到签到状态界面的常量

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.expand_signinfo_shidao_signphoto, null);
        ButterKnife.inject(this, view);
        dao = new CallNameDao(this);
        Bundle bundle = getArguments();
        ktid = bundle.getString("ktid");
        kcid = bundle.getString("kcid");
        Log.d("ktid" + ktid);
        PageConst.pageNo = 1;
        PageConst.pageSize = 1000;
        dao.LookClassInfo(ktid, PageConst.pageNo, PageConst.pageSize, CodeConst.EXPAND_SIGNINFO_SHIDAO_FRIST);
        return view;
    }

    /**
     * 初始化数据界面
     */
    private void initView() {

        List<Student> list = new ArrayList<>();
        for (int i = 0; i < dao.getStudentList().size(); i++) {
            if (dao.getStudentList().get(i).getQdzt().equals("2") && (dao.getStudentList().get(i).getPicpath() == null || dao.getStudentList().get(i).getPicpath().equals(""))) {
            } else {
                list.add(dao.getStudentList().get(i));
            }
        }
        Log.d("签到照片size=" + list.size());
        //空白页显示
        if (list.size() < 1) {
            setBlankView();
        } else {
            adapter = new SignPhotoAdapter(list, getActivity());
            photoGridView.setAdapter(adapter);

            adapter.setOnItemClickListener(new SignPhotoAdapter.OnItemClickListener() {
                @Override
                public void onClick(Student student, int position) {
                    //跳转到签到状态界面
                    Intent intent = new Intent(getActivity(), SigninStateActivity.class);
                    intent.putExtra("xm", student.getXm());
                    intent.putExtra("bj", student.getBj());
                    intent.putExtra("qdzt", student.getQdzt());
                    intent.putExtra("qdfs", student.getQdfs());
                    intent.putExtra("qdsj", student.getQdsj());
                    intent.putExtra("pic", student.getPicpath());

                    intent.putExtra("xh", student.getXh());
                    intent.putExtra("kcid", kcid);
                    intent.putExtra("ktid", ktid);
                    startActivityForResult(intent, INTENT_SIGNINSTATE);
                    AnimUtil.intentSlidIn((getActivity()));
                }
            });

        }
    }

    /**
     * 列表无数据，空白页的设置
     */
    void setBlankView() {
        Log.d("无数据显示");
        RelativeLayout layout = (RelativeLayout) view.findViewById(R.id.blank_view);
        layout.setVisibility(View.VISIBLE);
        TextView textView = (TextView) view.findViewById(R.id.tipBlank);//提示文字
        textView.setText("目前没有学生签到，稍后再来查看~");

        view.findViewById(R.id.grid).setVisibility(View.GONE);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == INTENT_SIGNINSTATE) {
            //从签到状态界面返回
            dao.LookClassInfo(ktid, PageConst.pageNo, PageConst.pageSize, CodeConst.EXPAND_SIGNINFO_SHIDAO_FRIST);
        }
    }

    @Override
    public void onRequestSuccess(int requestCode) {
        try {
            if (dao.getStatus().equals("0")) {
                switch (requestCode) {
                    case CodeConst.EXPAND_SIGNINFO_SHIDAO_FRIST:
                        //设置签到人数
                        ShiDaoFragment.countRen.setText(dao.getClassInfo().getQdrs());
                        initView();
                        break;
                }
            } else {
                MessageUtils.showImageShortToast(getActivity(), dao.getStatusCode());
            }
        } catch (Exception e) {
            MessageUtils.showImageShortToast(getActivity(), "请求数据异常");
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }
}
